package com.bdqn.ch01;

import com.bdqn.pojo.Role;
import com.bdqn.pojo.User;
import com.bdqn.repository.UserRepository;
import com.bdqn.vo.UserInfo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.List;

/**
 * UserRepositoryTester 类
 *
 * @author 玲玲
 * @Since 2023-11-23
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTester {
    @Resource
    private UserRepository userRepository;

    @Test
    public void testInsert(){
        //测试新增
        Role role=new Role();
        role.setRoleId(9L);
        User user=new User("JPA","123456",role,1);
        userRepository.save(user);
    }

    @Test
    public void testGet(){
        //测试按主键查询
        User user=userRepository.findById(2L).get();
        System.out.println("UsrName:"+user.getUsrName());
        System.out.println("roleName:"+user.getRole().getRoleName());
    }

    @Test
    public void testFindByUsrName(){
        //测试按用户名称模糊查询
        List<User> list=userRepository.findByUsrNameLike("%s%");//查询参数必须带%号
        if(list!=null){
            for (User user:list){
                System.out.println("UsrName:"+user.getUsrName());
            }
        }
    }
    //分页查询
    @Test
    public void testFindPageByUsrRoleId(){
        int page=0,size=2;//分页要素从0也开始d
//控制分页数据的排序,可以选择升序和降序
        Sort sort= Sort.by(Sort.Direction.DESC,"usrId");
//控制分页的辅助类,可以设置页码、每页的数据条数、排序等
        Pageable pageable= PageRequest.of(page,size,sort);
        Page<User> userPage=userRepository.findPageByUsrRoleId(9L,pageable);
        System.out.println("总记录数："+userPage.getTotalElements());
        System.out.println("总页数："+userPage.getTotalPages());
        System.out.println("当前页数："+(userPage.getNumber()+1));
        System.out.println("每页记录数："+userPage.getSize());
        System.out.println("当前页数记录数："+userPage.getNumberOfElements());
//获得查询记录userPage.getContent()
        for (User user : userPage.getContent()) {
            System.out.println("usrId："+user.getUsrId());
        }
    }
//动态分页查询

    @Test
    public void testFindPageByMap(){
        int page=0,size=2;//分页要素从0也开始d
//控制分页数据的排序,可以选择升序和降序
        Sort sort= Sort.by(Sort.Direction.DESC,"usrId");
//控制分页的辅助类,可以设置页码、每页的数据条数、排序等
        Pageable pageable= PageRequest.of(page,size,sort);
        Page<User> userPage=userRepository.findPageByUsrRoleId(1L,pageable);
        System.out.println("总记录数："+userPage.getTotalElements());
        System.out.println("总页数："+userPage.getTotalPages());
        System.out.println("当前页数："+(userPage.getNumber()+1));
        System.out.println("每页记录数："+userPage.getSize());
        System.out.println("当前页数记录数："+userPage.getNumberOfElements());
//获得查询记录userPage.getContent()
        for (User user : userPage.getContent()) {
            System.out.println("usrId："+user.getUsrId());
        }
    }

    @Test
    public void testGetUserInfo(){
        //测试多表联接查询
         UserInfo userInfo= userRepository.getUserInfo(2L);
        System.out.println("usrName:"+userInfo.getUsrName());
        System.out.println("roleName:"+userInfo.getRoleName());
    }


 }
